﻿using System.ServiceModel.Syndication;
using System.Web.Mvc;
using System.Xml;

namespace RecruitmentAgency.Models.Utilities
{
    /// <summary>
    /// Provides a simple mechanism to create RSS feeds in ATOM1.0 format as ActionResults
    /// </summary>
    public class RssFeedActionResult : ActionResult
    {
        /// <summary>
        /// The SyndicationFeed object that holds the data for the RSS feed
        /// </summary>
        public SyndicationFeed Feed { get; set; }

        /// <summary>
        /// Creates an RSS feed in ATOM1.0 format from the Feed property and writes it to the HttpContext.Response
        /// </summary>
        /// <param name="context">The context of the calling Controller class</param>
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";
            Atom10FeedFormatter rssFormatter = new Atom10FeedFormatter(Feed);
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }
}